﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WhatSNew.AppCode;

namespace WhatSNew.Manage
{
    public partial class NewsManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            UserInfo userInfo = (UserInfo)Session["user"];
            if (userInfo == null || userInfo.MInfo.UType == UserType.RegisterUser)
            {
                Response.Redirect("../User/Personal.aspx");
                return;
            }
            if (!IsPostBack)
            {
                NewsInfoAccess newsAccess = new NewsInfoAccess();
                UserInfoAccess userAccess = new UserInfoAccess();
                List<News> unAuditNews = newsAccess.GetUnauditNews();
                Session["unAuditNews"] = unAuditNews;
                int userBelong = unAuditNews[0].UserBelong;
                UserInfo user = new UserInfo();
                if (userBelong != -1)
                {
                    user = userAccess.GetUserInfo(userBelong);
                    Name1.Text = user.PInfo.Name;
                }
                else
                    Name2.Text = "匿名";
                Title1.Text = unAuditNews[0].Title;
                Text1.Text = unAuditNews[0].Content;
                Session["news1"] = unAuditNews[0];
                unAuditNews.RemoveAt(0);

                if (userBelong != -1)
                {
                    user = userAccess.GetUserInfo(userBelong);
                    Name2.Text = user.PInfo.Name;
                }
                else
                    Name2.Text = "匿名";
                Title2.Text = unAuditNews[0].Title;
                Text2.Text = unAuditNews[0].Content;
                Session["news2"] = unAuditNews[0];
                unAuditNews.RemoveAt(0);

                if (userBelong != -1)
                {
                    user = userAccess.GetUserInfo(userBelong);
                    Name3.Text = user.PInfo.Name;
                }
                else
                    Name2.Text = "匿名";
                Title3.Text = unAuditNews[0].Title;
                Text3.Text = unAuditNews[0].Content;
                Session["news3"] = unAuditNews[0];
                unAuditNews.RemoveAt(0);
            }
        }

        protected void Pass1_Click(object sender, EventArgs e)
        {
            UserInfo userInfo = (UserInfo)Session["user"];
            if (userInfo.MInfo.AuditPriv != 0)
            {
                Response.Write("<script language='javascript'>alert('你没有审核权限！');</script>");
                return;
            }
            List<News> unAuditNews = (List<News>)Session["unAuditNews"];
            NewsInfoAccess newsAccess = new NewsInfoAccess();
            UserInfoAccess userAccess = new UserInfoAccess();
            HistoryAccess historyAccess = new HistoryAccess();

            ManageHistory history = new ManageHistory();
            News news = (News)Session["news1"];
            //更新新鲜事状态
            newsAccess.UpdateState(news.Id, NewsStateType.Pass);
            //插入管理记录
            history.Action = ActionType.LetPass;
            history.ObjectUser = news.UserBelong;
            history.ManageObject = ObjectType.ANews;
            history.Administrator = userInfo.PInfo.ID;
            string curTime = DateTime.Now.ToString();
            history.Time = curTime;
            historyAccess.Insert(history);

            //重新读取未审批的新鲜事
            if (unAuditNews.Count == 0)
            {
                unAuditNews = newsAccess.GetUnauditNews();
                if (unAuditNews.Count == 0)
                {
                    Panel1.Visible = false;
                    return;
                }
            }
            int userBelong = unAuditNews[0].UserBelong;
            UserInfo user = new UserInfo();
            if (userBelong != -1)
            {
                user = userAccess.GetUserInfo(userBelong);
                Name1.Text = user.PInfo.Name;
            }
            else
                Name2.Text = "匿名";
            Title1.Text = unAuditNews[0].Title;
            Text1.Text = unAuditNews[0].Content;
            Session["news1"] = unAuditNews[0];
            unAuditNews.RemoveAt(0);
        }

        protected void Reject1_Click(object sender, EventArgs e)
        {

        }
    }
}